const Mock = require('mockjs')
const data = Mock.mock({
  'roleList|12': [{
    roleId: '@id',
    roleName: '@ctitle(0,30)',
    roleCode: '@integer(8,12)',
    statusFlag: '@integer(1,2)',
    followApp: '@integer(12,20)',
    followAppName: '@ctitle',
    grantOrgScopeIds: '0',
    dataScopeType: '4',
    createUser: '@cname()',
    createTime: '@time("HH:mm:ss")',
    updateUser: '@cname()',
    updateTime: '@time("HH:mm:ss")'
  }],
  allFunctionList: [
    { // 应用级别
      id: '1667064567054282753',
      nodeParentid: '-1',
      label: '系统配置',
      levelType: 1,
      checked: true,
      appCode: 'systemConfig',
      children: [
        { // 菜单级别
          id: '1667064567263998070',
          nodeParentId: '1667064567054282753',
          label: '基础配置',
          checked: true,
          levelType: 2,
          menuCode: 'systemConfig',
          children: [
            { // 按钮级别
              id: '1667064567268192359',
              nodeParentId: '1667064567263998070',
              label: '新增',
              levelType: 2,
              checked: true,
              buttonCode: 'button',
              children: [

              ]
            }
          ]
        }
      ]
    },
    {
      id: '1667064567054282750',
      nodeParentid: '-1',
      label: '租户管理',
      levelType: 1,
      checked: false,
      appCode: 'systemConfig',
      children: [
        { // 菜单级别
          id: '1667064567263998074',
          nodeParentId: '1667064567054282750',
          label: '租户管理',
          checked: false,
          levelType: 2,
          menuCode: 'systemConfig',
          children: [
            { // 按钮级别
              id: '1667064567268192354',
              nodeParentId: '1667064567263998074',
              label: '新增',
              levelType: 3,
              checked: false,
              buttonCode: 'button',
              children: [
                { // 按钮级别
                  id: '1667064567268192456',
                  nodeParentId: '1667064567268192354',
                  label: '第三方功能',
                  levelType: 4,
                  checked: false,
                  functionCode: 'functionCode',
                  children: []
                }
              ]
            },
            {
              id: '1667064567268192355',
              nodeParentId: '1667064567263998074',
              label: '修改',
              levelType: 3,
              checked: false,
              buttonCode: 'button',
              children: []
            },
            {
              id: '1667064567268192356',
              nodeParentId: '1667064567263998074',
              label: '查看',
              levelType: 3,
              checked: false,
              buttonCode: 'button',
              children: []
            }
          ]
        }
      ]
    }
  ],
  choosedFunctionList: [
    { // 应用级别
      id: '1667064567054282753',
      nodeParentid: '-1',
      label: '系统配置',
      levelType: 1,
      checked: true,
      appCode: 'systemConfig',
      children: [
        { // 菜单级别
          id: '1667064567263998070',
          nodeParentId: '1667064567054282753',
          label: '基础配置',
          checked: true,
          levelType: 2,
          menuCode: 'systemConfig',
          children: [
            { // 按钮级别
              id: '1667064567268192359',
              nodeParentId: '1667064567263998070',
              label: '新增',
              levelType: 3,
              checked: true,
              buttonCode: 'button',
              children: [

              ]
            }
          ]
        }
      ]
    }
  ],
  'userList|12': [{
    userId: '@id()',
    realName: '@cname()',
    account: '@first()',
    positionName: '软件工程师',
    orgId: '@id',
    orgName: '河钢数字'
  }],
  'appList|12': [{
    appId: '@id()',
    appName: '@cname()',
    appCode: '@integer(12,20)'
  }],
  orgData: [{
    orgId: 0,
    orgParentId: '@id',
    orgName: '河钢集团',
    orgFullPath: '@cname',
    children: [{
      orgId: 1,
      orgParentId: '@id',
      orgName: '@cname',
      orgFullPath: '@cname',
      children: [{
        orgId: 2,
        orgParentId: '@id',
        orgName: '@cname',
        orgFullPath: '@cname'
      }]
    }]
  }]
})
module.exports = [
  // 查询角色列表
  {
    url: '/permission/businessRole/rolePage',
    type: 'get',
    response: (config) => {
      // const { pageNo = 1, pageSize = 10 } = config.query
      // const pageList = items.filter((item, index) => index < pageSize * pageNo && index >= pageSize * (pageNo - 1))
      const { roleName } = config.query
      const { followApp } = config.query
      const items = data.roleList.filter((item) => {
        return item.roleName.indexOf(roleName) !== -1 && item.followApp !== followApp
      })
      return {
        code: '00000',
        data: {
          pageNo: 1,
          pageSize: 10,
          totalPage: Math.ceil(items.length),
          totalRows: items.length,
          rows: items
        },
        message: '请求成功',
        success: true
      }
    }
  },
  // 查询所有第三方应用列表
  {
    url: '/permission/businessRole/getCurrAdminGroupApps',
    type: 'get',
    response: (config) => {
      return {
        code: '00000',
        data: data.appList,
        message: '请求成功',
        success: true
      }
    }
  },
  // 业务角色详情/编辑展示接口
  {
    url: '/permission/businessRole/detail',
    type: 'get',
    response: (config) => {
      const { roleList } = data
      const { roleId } = config.body
      for (const item of roleList) {
        if (item.roleId === roleId) {
          const data = Mock.mock({
            userInfo: {
              roleId: item.roleId,
              roleName: item.roleName,
              roleCode: item.roleCode,
              roleType: '@integer(1, 2)',
              statusFlag: item.statusFlag,
              followApp: item.followApp,
              remark: '@csentence()',
              grantOrgScopeIds: [
                {
                  id: '1673934596313837570',
                  grantOrgName: '战略发展部',
                  grantOrgPath: '河钢数字/战略发展本部',
                  open: true,
                  checked: true,
                  iconSkin: null,
                  children: null,
                  nodeParentId: '1667064405735841794'
                },
                {
                  id: '1673934596313837570',
                  grantOrgName: '工业互联网平台',
                  grantOrgPath: '河钢数字/工业互联网平台',
                  open: true,
                  checked: true,
                  iconSkin: null,
                  children: null,
                  nodeParentId: '1667064405735841794'
                }
              ],
              dataScopeType: '@integer(1, 4)',
              createUser: '@cname()',
              createTime: '@time("HH:mm:ss")',
              updateUser: '@cname()',
              updateTime: '@time("HH:mm:ss")'
            }
          })
          return {
            code: '00000',
            data: data.userInfo,
            message: '请求成功',
            success: true
          }
        }
      }
    }
  },
  //
  // 查询角色已选中功能权限列表
  {
    url: '/permission/businessRole/getRoleFunctionChooseTree',
    type: 'get',
    response: (config) => {
      const { roleList } = data
      const { roleId } = config.query
      for (const item of roleList) {
        if (item.roleId === roleId) {
          return {
            code: '00000',
            data: data.choosedFunctionList,
            message: '请求成功',
            success: true
          }
        }
      }
    }
  },
  // 获取角色授权用户列表
  {
    url: '/permission/businessRole/getRoleUserPage',
    type: 'get',
    response: (config) => {
      const items = data.userList
      const { pageNo = 1, pageSize = 10 } = config.query
      const pageList = items.filter((item, index) => index < pageSize * pageNo && index >= pageSize * (pageNo - 1))
      return {
        code: '00000',
        data: {
          pageNo: 1,
          pageSize: 10,
          totalPage: Math.ceil(items.length),
          totalRows: items.length,
          rows: pageList
        },
        message: '请求成功',
        success: true
      }
    }
  },
  // 删除、批量删除授权用户
  {
    url: '/permission/businessRole/batchDeleteUsers',
    type: 'post',
    response: (config) => {
      const { userList } = data
      // const roleId = config.body.roleId
      const userIds = config.body.userIds.split(',')
      if (userIds.length > 1) {
        userList.splice(userIds, userIds.length)
      } else {
        userList.map((item, index) => {
          userIds.map((val) => {
            if (val === item.userId) {
              userList.splice(index, 1)
            }
          })
        })
      }
      return {
        code: '00000',
        data: userList,
        message: '请求成功',
        success: true
      }
    }
  },
  // 改变角色的状态
  {
    url: '/permission/businessRole/updteStatus',
    type: 'post',
    response: (config) => {
      const items = data.roleList
      const { roleId } = config.body
      const { statusFlag } = config.body
      for (const item of items) {
        if (item.roleId === roleId) {
          item.statusFlag = statusFlag
          return {
            code: '00000',
            data: null,
            message: '请求成功',
            success: true
          }
        }
      }
    }
  },
  // 删除角色
  {
    url: '/permission/businessRole/deleteRole',
    type: 'post',
    response: (config) => {
      const items = data.roleList
      const { roleId } = config.body
      items.map((item, index) => {
        if (item.roleId === roleId) {
          items.splice(index, 1)
        }
      })
      return {
        code: '00000',
        data: items,
        message: '请求成功',
        success: true
      }
    }
  },
  // 下载三方角色导入模板
  {
    url: '/businessRole/downloadTemplate',
    type: 'get',
    responseType: 'arraybuffer',
    response: (_) => {
      const { userList } = data
      // const file = Mock.mock('http://192.168.100.172:30377/stage-api/usercenter/sysUser/downloadTemplate?enclosure=%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xls')
      const user = Mock.mock({
        roleId: '@id',
        roleName: '小默',
        roleCode: '@integer(8,12)',
        statusFlag: '@integer(1,2)',
        followApp: '@integer(12,20)',
        grantOrgScopeIds: '@range(1, 10, 2)',
        dataScopeType: '@integer(1, 4)',
        createUser: '@cname()',
        createTime: '@time("HH:mm:ss")',
        updateUser: '@cname()',
        updateTime: '@time("HH:mm:ss")'
      })
      userList.push(user)
      return {
        code: '00000',
        data: userList,
        message: '请求成功',
        success: true
      }
    }
  },
  // 导入三方角色
  {
    url: '/businessRole/uploadRoles',
    type: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    response: (_) => {
      return {
        code: '00000',
        data: null,
        message: '请求成功',
        success: true
      }
    }
  },
  // 查询角色可选功能权限列表
  {
    url: '/permission/businessRole/getRoleFunctionTree',
    type: 'get',
    response: (config) => {
      const { followApp, roleType, roleId } = config.query
      const { allFunctionList } = data
      if (roleType || followApp || roleId) {
        return {
          code: '00000',
          data: allFunctionList,
          message: '请求成功',
          success: true
        }
      }
    }
  },
  // 查询角色可选数据范围列表
  {
    url: '/permission/businessRole/getRoleDataScopeChildren',
    type: 'get',
    response: (config) => {
      // const { roleId, orgName, orgId } = config.query
      const { orgData } = data
      return {
        code: '00000',
        data: orgData,
        message: '请求成功',
        success: true
      }
    }
  },
  // 新增角色
  {
    url: '/permission/businessRole/add',
    type: 'post',
    response: (config) => {
      config.body.roleId = Mock.mock({ roleId: '@id' })
      const roleList = data.roleList.push(config.body)
      return {
        code: '00000',
        data: roleList,
        message: '请求成功',
        success: true
      }
    }
  },
  // 修改角色
  {
    url: '/permission/businessRole/update',
    type: 'post',
    response: (config) => {
      const roleList = data.roleList.map((item, index) => {
        if (item.roleId === config.body.roleId) {
          data.roleList[index] = config.body
          return data.roleList
        }
      })
      return {
        code: '00000',
        data: roleList,
        message: '请求成功',
        success: true
      }
    }
  },
  // 复制角色
  {
    url: '/permission/businessRole/copy',
    type: 'post',
    response: (config) => {
      const roleList = data.roleList.map((item, index) => {
        if (item.roleId === config.body.roleId) {
          config.body.roleId = '@id'
          data.roleList.push(config.body)
          return data.roleList
        }
      })
      data.roleList.push(config.body)
      return {
        code: '00000',
        data: roleList,
        message: '请求成功',
        success: true
      }
    }
  },
  // 授权用户保存
  {
    url: '/permission/businessRole/grantRoleUsers',
    type: 'post',
    response: (config) => {
      // const userList = data.userList.push(config.body)
      if (config.query.roleId) {
        config.query.userIds.split(',').map((item) => {
          const user = Mock.mock({
            userId: item,
            realName: '@cname()',
            account: '@first()',
            positionName: '软件工程师',
            orgId: '@id',
            orgName: '河钢数字'

          })
          data.userList.push(user)
        })
      }
      return {
        code: '00000',
        data: data.userList,
        message: '请求成功',
        success: true
      }
    }
  }

]
